package com.example.mobilesafe1221.home.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.example.mobilesafe1221.R;

import java.io.InputStream;

public class ArcProgressBar extends View {
    private Paint paint;       //画笔对象的引用
    private int textColor;    //中间进度百分比进度的文字颜色
    private float textSize;   //中间进度百分比进度的文字大小
    private int max;           //进度条的最大值
    private int progress;     //当前进度
    private boolean isDisplayText; //是否显示中间百分比字符串
    private String title; //标题
    private Bitmap bitmap; //栅格图类
    private int degrees;  //度数
    private Bitmap bmpTemp = null;

    public ArcProgressBar(Context context) {
        this(context, null);
    }

    public ArcProgressBar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ArcProgressBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        //   创建 画笔 工具
        degrees=0;
        paint=new Paint();

        //获取自定义属性和默认值--style.xml
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.XCRoundProgressBar);
        //设置颜色
        textColor = typedArray.getColor(R.styleable.XCRoundProgressBar_textColor, Color.parseColor("#EED2EE"));
        //设置大小
        textSize = typedArray.getDimension(R.styleable.XCRoundProgressBar_textSize, 50);
        //圆环最大进度
        max = typedArray.getInteger(R.styleable.XCRoundProgressBar_max, 100);
        //是否可见
        isDisplayText = typedArray.getBoolean(R.styleable.XCRoundProgressBar_textIsDisplay, true);
        typedArray.recycle();  //可多次使用


    }

    /**
     * @param canvas
     */
    @Override           //重写onDraw方法
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int width = getWidth();
        int height = getHeight();
        int centerX = getWidth() / 2; //获取中心点X坐标
        int centerY = getHeight() / 2;  //获取中心点y坐标
        //根据宽和高，创建栅格图
        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        //A表示透明度 RGB红绿蓝
        Canvas cv = new Canvas(bitmap);
        //绘制底部背景图
        bmpTemp = decodeCustomRes(getContext(), R.drawable.arc_bg);
        float dstWidth = (float) width;  //目标图片的宽度
        float dstHeight = (float) height; //目标图片 的高度
        int srcWidth = bmpTemp.getWidth(); //获取原图的宽度   100
        int srcHeight = bmpTemp.getHeight();  //获取原图的高度 150
        //创建一个抗锯齿的栅格图
        //绘制背景圆环
        Bitmap bmpbg = Bitmap.createScaledBitmap(bmpTemp, width, height, true);
        //放到画布上,进行绘制
        cv.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG | Paint.FAKE_BOLD_TEXT_FLAG));//抗锯齿

        cv.drawBitmap(bmpbg, 0, 0, null);


        //绘制前景图
        //创建一个矩形对象
        Matrix matrixProgress = new Matrix();//3*3矩形，平移，旋转，缩放
        matrixProgress.postScale(dstWidth / srcWidth, dstHeight / srcHeight);//缩放
        //白色圆环转化为栅格图
        bmpTemp = decodeCustomRes(getContext(), R.drawable.arc_progress);

        //创建进度条
        Bitmap bmpProgress = Bitmap.createBitmap(bmpTemp, 0, 0, srcWidth, srcHeight, matrixProgress, true);
        //计算度数
        degrees = progress * 270 / max - 270;
        //遮罩 处理前景与背景图片
        cv.save();


        cv.rotate(degrees, centerX, centerY);// centerX=70   centerY=75 degrees 度数

        paint.setAntiAlias(true);//设定抗锯齿
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_ATOP));

        cv.drawBitmap(bmpProgress, 0, 0, paint);
        cv.restore();
        //大于85度的时候图坐标情况 ，大于255度时候依照坐标绘制目标图与源图
        if ((-degrees) >= 85) {
            int posX = 0;
            int posY = 0;
            if ((degrees) >= 270) {
                posX = 0;
                posY = 0;

            } else if ((-degrees) >= 225) {
                posX = centerX / 2;
                posY = 0;

            } else if ((-degrees) >= 180) {
                posX = centerX;
                posY = 0;

            } else if ((-degrees) >= 135) {
                posX = centerX;
                posY = 0;

            } else if ((-degrees) >= 85) {
                posX = centerX;
                posY = centerY;

            }
            //依照坐标绘制目标图与源图
            if ((-degrees) >= 255) {
                cv.save();//锁定画布，依旧最原始的图，创建目标图
                Bitmap dst = bitmap.createBitmap(bitmap, 0, 0, centerX, centerX);
                paint.setAntiAlias(true);//抗锯齿效果
                paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));//两张图叠加的时候

                Bitmap src = bmpbg.createBitmap(bmpbg, 0, 0, centerX, centerY);
                cv.drawBitmap(src, 0, 0, paint);
                cv.restore();
                cv.save();


                dst = bitmap.createBitmap(bitmap, centerX, 0, centerX, height);
                paint.setAntiAlias(true);
                paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));
                src = bmpbg.createBitmap(bmpbg, centerX, 0, centerX, height);
                cv.drawBitmap(src, centerX, 0, paint);
                cv.restore();

            } else {
                cv.save();
                Bitmap dst = bitmap.createBitmap(bitmap, posX, posY, width - posX, height - posY);
                paint.setAntiAlias(true);
                paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));
                Bitmap src = bmpbg.createBitmap(bmpbg, posX, posY, width - posX, height - posY);

                cv.drawBitmap(src, posX, posY, paint);
                cv.restore();
            }
        }
        //绘制遮罩层位图
        canvas.drawBitmap(bitmap,0,0,null);
        //画中间进度百分比字符串
        paint.reset();
        paint.setStrokeWidth(0);  //空心的图
        paint.setColor(textColor);//设置文字颜色
        paint.setTextSize(textSize);//设置文字大小
        paint.setTypeface(Typeface.DEFAULT_BOLD); //设置文字样式
        //计算百分比
        int percent =(int ) (((float)progress/(float)max)*100) ;
        //计算文字宽度
        float textWidth =paint.measureText(percent+"%");
        if (isDisplayText && percent !=0 ){

            canvas.drawText(percent +"%",centerX-textWidth/2,centerX+textSize/2-25,paint);

        }

        //画底部开口标题
        paint.setTextSize(textSize/2);
        textWidth =paint.measureText(title) ;
        canvas.drawText(title,centerX-textWidth/2,height-textSize/2,paint);

    }

    /**
     * 从Resource中获取图片资源，转化为Bitmap类型的数据返回
     * @param c   上下文
     * @param res   资源
     * @return
     */
    public static  Bitmap decodeCustomRes(Context c,int res){
        InputStream is=c.getResources().openRawResource(res);
        BitmapFactory.Options options =new BitmapFactory.Options(); //创建一个栅格布局
        options.inJustDecodeBounds=false; //将图片加载到内存
        options.inSampleSize=1 ; //原尺寸加载图片
        Bitmap bmp =BitmapFactory.decodeStream(is) ; //解码数据流 encode编码 decode解码
        return  bmp ;

    }
//私有属性的set 和get 方法


    public Paint getPaint() {
        return paint;
    }

    public void setPaint(Paint paint) {
        this.paint = paint;
    }

    public int getTextColor() {
        return textColor;
    }

    public void setTextColor(int textColor) {
        this.textColor = textColor;
    }

    public float getTextSize() {
        return textSize;
    }

    public void setTextSize(float textSize) {
        this.textSize = textSize;
    }

    public boolean isDisplayText() {
        return isDisplayText;
    }

    public void setDisplayText(boolean displayText) {
        isDisplayText = displayText;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }


    public int getMax() {
        return max;
    }

    public void setMax(int max) {  //进度条最大值100
        if (max<0){
            throw  new IllegalArgumentException("max must more than 0");
        }
        this.max = max;
    }
    public int getProgress() {
        return progress;
    }
    public void setProgress(int progress) { //progress当前进度

        if (progress<0){
            throw  new IllegalArgumentException("pogree must be more than 0") ;
        }
        this.progress = progress;
    }

}
